package com.ll.studentservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ll.studentservice.entity.EduStartWeek;
import com.ll.studentservice.mapper.EduStartWeekMapper;
import com.ll.studentservice.service.EduStartWeekService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.junit.Test;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ll
 * @since 2020-12-07
 */
@Service
public class EduStartWeekServiceImpl extends ServiceImpl<EduStartWeekMapper, EduStartWeek> implements EduStartWeekService {

    // 获取现在是第几周
    @Override
    public String getNowWeek() {
        String nowDate = LocalDate.now().toString();

        QueryWrapper<EduStartWeek> wrapper = new QueryWrapper<>();
        //学期开始时间小于等于现在时间
        wrapper.le("semester_start",nowDate);
        //学期结束时间大于等于现在时间
        wrapper.ge("semester_end", nowDate);

        EduStartWeek eduStartWeek = baseMapper.selectOne(wrapper);

        String semesterStart = eduStartWeek.getSemesterStart();

        System.out.println("学期开始时间" + semesterStart);

        //格式转换
        LocalDate parse = LocalDate.parse(semesterStart);
        LocalDate now = LocalDate.now();

        //现在时间和学期开始时间相差多少天
        int day = (int) (now.toEpochDay() - parse.toEpochDay());

        //现在第几周
        int nowWeek = 0;

        if(day % 7 > 0){
            nowWeek =  day / 7 + 1;

        } else {
            nowWeek =  day / 7;
        }

        return nowWeek + "";
    }
}
